package cn.kgc.controller;

import cn.kgc.base.dto.UmsRoleDTO;
import cn.kgc.client.UmsRoleFeignClient;
import cn.kgc.commons.annotation.AutoDict;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.UserContextHolder;
import cn.kgc.commons.vo.Result;
import cn.kgc.dto.add.WmsPurchaseAddDTO;
import cn.kgc.entity.WmsPurchase;
import cn.kgc.entity.WmsWareInfo;
import cn.kgc.mapper.WmsPurchaseDetailMapper;
import cn.kgc.service.WmsPurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>采购单控制器</p>
 *
 * @author 李孟鸠
 * @Date: 2022年12月07日11:32
 */
@RestController
@Api(tags = "采购单控制器")
@RequestMapping("/wms/purchase")
@Validated
@Slf4j
public class WmsPurchaseController {

    @Resource
    private WmsPurchaseService wmsPurchaseService;

    @Resource
    private UmsRoleFeignClient umsRoleFeignClient;

    @PostMapping("/save")
    @CrossOrigin
    @ApiOperation(value = "新建采购单",httpMethod = "POST",produces = "application/json;charset=UTF-8",response = Result.class)
    public boolean saveWmsPurchase(@RequestBody WmsPurchaseAddDTO wmsPurchaseAddDTO){
        return wmsPurchaseService.saveWmsPurchase(wmsPurchaseAddDTO);
    }

    //采购人员接收采购任务 采购人员可以看到自己的采购单 状态改为2



    //采购管理员将采购单分配给采购人员
    //能不能将采购单重新分配给别人
    @PostMapping("/allocation")
    @CrossOrigin
    @ApiOperation(value = "采购单分配",httpMethod = "POST",produces = "application/json;charset=UTF-8",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "采购单id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "assigneeId",value = "采购人id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "采购人手机号",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "assigneeName",value = "采购人名字",dataType = "string",paramType = "query"),
    })
    public boolean allocation(@RequestParam(name = "assigneeId") Long assigneeId,
                              @RequestParam(name = "phone") String phone,
                              @RequestParam(name = "assigneeName") String assigneeName,
                              @RequestParam(name = "id") Long id){

        return wmsPurchaseService.allocationWmsPurchase(assigneeId, assigneeName, phone,id);
    }


    //查看自己的采购单信息，分页查询(默认当前登录的用户的ID)
    //普通采购员可以查看自己的采购单，采购的管理人员，查看所有的采购单订单
    @CrossOrigin
    @GetMapping("/list")
    @ApiOperation(value = "查看采购单信息",httpMethod = "GET",produces = "application/json;charset=UTF-8",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "currentPage",value = "每页显示条数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "wmsPurchaseId",value = "订单id",dataType = "long",paramType = "query")
    })
    @AutoDict
    public IPage<WmsPurchase> selectAll(@RequestParam(defaultValue = "5") int pageSize,
                                        @RequestParam(defaultValue = "1") int currentPage,
                                        @RequestParam(defaultValue = "0") long wmsPurchaseId){

        // long memberId =Long.parseLong( UserContextHolder.getInstance().getUserId());
        long memberId=1;
        //根据用户的ID获取用户的角色
        List<UmsRoleDTO>list = umsRoleFeignClient.getUmsRole(memberId).getData();
        if (list == null){
           throw new HttpException(40002,500);
        }
        //获取用户的角色
//        List<UmsRoleDTO>list= umsRoleFeignClient.getUmsRole(memberId).getData();
        boolean flag = false; //是否是采购管理员

        for (UmsRoleDTO umsRoleDTO : list) {
            if (umsRoleDTO.getRoleCode().equals("CGGLY")) {
                flag = true;
                break;
            }
        }
        //如果是采购管理员，就查询所有的采购单信息
        LambdaQueryWrapper<WmsPurchase>lambdaQueryWrapper=
                new LambdaQueryWrapper<>();
            //如果是采购管理员，能够查询所有的采购单信息，如果不是采购管理员，只能查看自己的

            if (!flag){ //不是采购管理员
                lambdaQueryWrapper.eq(WmsPurchase::getAssigneeId,memberId);
            }
            lambdaQueryWrapper.eq(wmsPurchaseId!=0,WmsPurchase::getId,wmsPurchaseId);



        IPage<WmsPurchase>page=new Page<>(currentPage,pageSize);

        return wmsPurchaseService.page(page,lambdaQueryWrapper);

    }


    @PostMapping("/finish")
    @CrossOrigin
    @ApiOperation(value = "采购单完成",httpMethod = "POST",produces = "application/json;charset=UTF-8",response = Result.class)
    @ApiImplicitParam(name = "id",value = "采购单id",dataType = "Long",paramType = "query")
    public boolean finishWmsPurchase(@RequestParam(name = "id") Long id){
        return wmsPurchaseService.finishWmsPurchase(id);
    }


    //acceptWmsPurchase

    @PostMapping("/accept")
    @ApiOperation(value = "采购单接收",httpMethod = "POST",
            produces = "application/json;charset=UTF-8",response = Result.class)
    @ApiImplicitParam(name="id",value = "采购单id",dataType = "long",paramType = "query")
    public boolean acceptWmsPurchase(@RequestParam(name = "id") Long id){
        return wmsPurchaseService.acceptWmsPurchase(id);
    }


}
